home *** CD-ROM | disk | FTP | other *** search
- Path: howland.reston.ans.net!psinntp!psinntp!psinntp!psinntp!usenet
- From: grantp@usa.pipeline.com(Pete Grant)
- Newsgroups: comp.lang.c++
- Subject: Re: object creation from an abstract base class
- Date: 24 Mar 1996 15:37:33 GMT
- Organization: Kalevi, Inc.
- Message-ID: <4j3q7t$f3i@news1.h1.usa.pipeline.com>
- References: <4j27q0$shm@arl-news-svc-3.compuserve.com>
- NNTP-Posting-Host: 38.8.120.9
- X-PipeUser: grantp
- X-PipeHub: usa.pipeline.com
- X-PipeGCOS: (Pete Grant)
- X-Newsreader: Pipeline v3.5.0
-
- On Mar 24, 1996 01:16:48 in article <Re: object creation from an abstract
- base class>, 'Philippe Verdy <100105.3120@compuserve.com>' wrote:
-
-
- >Wang TianXing <gztxwang@public1.guangzhou.gd.cn> s'Θcrit :
- >> On 23 Mar 1996 18:22:26 GMT, grantp@usa.pipeline.com(Pete Grant)
- >> wrote:
- >>
- >> | On Mar 21, 1996 08:59:50 in article <Re: object creation from an
- abstract
- >> | base class>, 'Sukanta Ganguly <sukanta_ganguly@novell.com>' wrote:
- >> |
- >> |
- >> | >Michael Catello wrote:
- >> | >>
- >> | >> Hello OOPsters,
- >> | >>
- >> | >> I was just looking for validation/other suggestions for a method I
-
- >> | >> recently used in a program. I have defined an abstract base class
- >> | >> (i.e. contains pure virtual functions), all access to the derived
- >> | >> classes of this base are thru a pointer to the base class. To
- create
- >> | >> the actual objects of the derived classes I used the following
- scheme:
- >> | >>
- >> | >> enum FooType {BAR, BAS};
- >> | >>
- >> | >> // base class
- >> | >> class CFoo
- >> | >> {
- >> | >> CFoo();
- >> | >> ~CFoo();
- >> | >>
- >> | >> static CFoo* CreateFoo(FooType type);
- >> | >>
- >> | >> // other methods/data including pure virtual fns whose
- >behaviour
- >> |
- >> | >will
- >> | >> be defined in the derived classes
- >> | >> };
- >> | >>
- >> | >> class CBar: public CFoo
- >> | >> {
- >> | >> //
- >> | >> };
- >> | >>
- >> | >> class CBas: public CFoo
- >> | >> {
- >> | >> //
- >> | >> };
- >> | >>
- >> | >> CFoo* CFoo::CreateFoo(FooType type)
- >> | >> {
- >> | >> CFoo* pfoo = NULL;
- >> | >>
- >> | >> switch (type)
- >> | >> {
- >> | >> case BAR:
- >> | >> pfoo = new CBar;
- >> | >> break;
- >> | >> case BAS:
- >> | >> pfoo = new CBas;
- >> | >> break;
- >> | >> }
- >> | >>
- >> | >> return pfoo;
- >> | >> }
- >> | >>
- >> | >> main()
- >> | >> {
- >> | >> CFoo* interface = CFoo::CreateFoo(BAR);
- >> | >> }
- >> | >>
- >> | >> Obviously it is the CreateFoo() function that I am wondering about.
- In
- >> | >> the actual implementation I had multiple static "Create" functions
- for
- >> | >> the base class that would allow me to create a new object: one
- based
- >> | >> on an enumerated token (shown above), another an existing object,
- as
- >> | >> well as one based on the format of a datafile. My application never
-
- >> | >> references any of the derived classes directly, except in their
- >> | >> creation and definition.
- >> | >>
- >> | >> Is there another/better/more appropriate way to handle this type of
-
- >> | >> object creation? Thanks for your assistance,
- >> | >>
- >> | > [incorrect things deleted]
- >> |
- >> | BTW, the statement "pfoo = new CBas;" is perfectly legal,
- >> | and even proper. Any compiler that "cribs" about it is wrong.
- >>
- >> So, the original poster should write code like this:
- >>
- >> CFoo *interface = new CBar;
- >>
- >> and remove the static function CFoo::CreateFoo(FooType);
- >>
- >No ! We need such a function in many C++ programs which have
- >to manage presistent collections of objects. However, such
- >applications do not use an enumeration but a registration
- >base class which is called by adding a static member to each
- >derived class, which will register themselves and obtain a
- >valid Id collected on a persistent stream. But on the final
- >result, managing such collections require that we have a
- >CFoo::CreateFoo(FooType) function in order to recreate the
- >collection of objects from a source which only enumerates
- >FooTypes. This is useful to create containers of graphic
- >objects with common attributes, and varying parameters
- >stored on a persistent stream, so that we can load and draw
- >the collection of objects without knowing anything on them.
- >Your code is legal Wang but you can generalize it to avoid
- >the switch() and the enumeration.
- >Philippe Verdy
-
- I tried to condense this thread, but couldn't find anything to
- delete without losing content. Sorry about the bandwith use.
-
- Philippe:
-
- How can the code be generalized to avoid the switch()? I
- mean completely avoid it; not just hide it inside another
- class or function.
-
- I'm not familiar with the "registration base class" idiom.
- Where can I find more info on it? From your description, I
- get the impression that you generate the equivalent of an
- enumeration at run time -- but that can't be. If you
- generate a different ID the next time you run the program,
- your file becomes invalid. If you guarantee that the ID
- is the same each time, then it's just a substitute for
- enumeration.
-
- In any case, the bottom line appears to be that you still
- need to write the type info into the file, and upon restoration
- of objects, the type info must be examined and operator
- new called based on the type info.
-
- --
- Pete Grant
- Kalevi, Inc.
- Software Engineering & development
-